/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * Geophysical Computational Tools & Library (GCTL)
 *
 * Copyright (c) 2023  Yi Zhang (yizhang-geo@zju.edu.cn)
 *
 * GCTL is distributed under a dual licensing scheme. You can redistribute 
 * it and/or modify it under the terms of the GNU Lesser General Public 
 * License as published by the Free Software Foundation, either version 2 
 * of the License, or (at your option) any later version. You should have 
 * received a copy of the GNU Lesser General Public License along with this 
 * program. If not, see <http://www.gnu.org/licenses/>.
 * 
 * If the terms and conditions of the LGPL v.2. would prevent you from using 
 * the GCTL, please consider the option to obtain a commercial license for a 
 * fee. These licenses are offered by the GCTL's original author. As a rule, 
 * licenses are provided "as-is", unlimited in time for a one time fee. Please 
 * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget 
 * to include some description of your company and the realm of its activities. 
 * Also add information on how to contact you by electronic and paper mail.
 ******************************************************/

#ifndef _GCTL_VERTEX_H
#define _GCTL_VERTEX_H

#include "point2c.h"
#include "point2p.h"
#include "point3c.h"
#include "point3s.h"

namespace gctl
{
    // this variable is only valid in this source file to control
    // the output of a 2D or 3D vertex.
    static char vertex_delimiter = ' ';

    template <typename T, typename A = void> struct vertex;

    typedef vertex<point2dc> vertex2dc;
    typedef vertex<point2dp> vertex2dp;
    typedef vertex<point2fc> vertex2fc;
    typedef vertex<point2fp> vertex2fp;
    typedef vertex<point3dc> vertex3dc;
    typedef vertex<point3ds> vertex3ds;
    typedef vertex<point3fc> vertex3fc;
    typedef vertex<point3fs> vertex3fs;

    template <typename T, typename A>
    struct vertex : public T
    {
        int id;
        A *att;
        /**
         * Constructor
         */
        vertex();
        /**
         * @brief      Construct an object from a 2D point
         *
         * @param[in]  p     Input point
         * @param[in]  idx   Element's index. Must be equal or greater than 0.
         */
        vertex(const T &p, int idx = 0, A* att_ptr = nullptr);
        /**
         * @brief      Copy constructor
         *
         * @param[in]  b     Input vertex
         */
        vertex(const vertex<T, A> &b);
        /**
         * @brief      De-constructor
         */
        virtual ~vertex();
        /**
         * @brief      Set an object from a 2D point
         *
         * @param[in]  p     Input point
         * @param[in]  idx   Element's index. Must be equal or greater than 0.
         */
        void set(const T &p, int idx = -1, A* att_ptr = nullptr);
         /**
         * @brief      Copy an object
         *
         * @param[in]  b     Input vertex
         */
        void set(const vertex<T, A> &b);
        /**
         * @brief      Extract the vertex's location
         *
         * @return     The location
         */
        T get_loc() const;
        /**
         * @brief      Set delimiter for the output stream
         *
         * @param[in]  deli      delimiter
         */
        void set_delimiter(char deli);
    };

    template <typename T, typename A>
    vertex<T, A>::vertex()
    {
        id = 0;
        att = nullptr;
    }

    template <typename T, typename A>
    vertex<T, A>::vertex(const T &p, int idx, A* att_ptr)
    {
        set(p, idx, att_ptr);
    }

    template <typename T, typename A>
    vertex<T, A>::vertex(const vertex<T, A> &b)
    {
        set(b);
    }

    template <typename T, typename A>
    vertex<T, A>::~vertex(){}

    template <typename T, typename A>
    void vertex<T, A>::set(const T &p, int idx, A* att_ptr)
    {
        if (idx >= 0) id = idx;
        if (att_ptr != nullptr) att = att_ptr;
        T::set(p);
        return;
    }

    template <typename T, typename A>
    void vertex<T, A>::set(const vertex<T, A> &b)
    {
        id = b.id;
        att = b.att;
        T::set(b); // call T's set function by force
    }

    template <typename T, typename A>
    T vertex<T, A>::get_loc() const
    {
        return T::get_loc();
    }

    template <typename T, typename A>
    void vertex<T, A>::set_delimiter(char deli)
    {
        vertex_delimiter = deli;
        return;
    }

    template <typename T, typename A>
    std::ostream &operator <<(std::ostream & os, const vertex<T, A> &a)
    {
        os << a.id << vertex_delimiter; a.out_loc(os, vertex_delimiter);
        return os;
    }

    template <typename T, typename A>
    std::istream &operator >>(std::istream & os, vertex<T, A> &a)
    {
        os >> a.id; a.in_loc(os);
        return os;
    }
}

#endif // _GCTL_VERTEX_H